<?php
// Stop direct call
if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) { die('You are not allowed to call this page directly.'); }

/**
 * Setup the default option array for the gallery
 * 
 * @access internal
 * @return void
 */
function grand_default_options() {
	
	global $blog_id, $grandLoad;

	$grand['imagePath']			= '/'.GRAND_FOLDER.'/images/'; 			// set default path to the galleries relative to wp-content dir
	$grand['musicPath']			= '/'.GRAND_FOLDER.'/music/';  			
	$grand['videoPath']			= '/'.GRAND_FOLDER.'/video/';  			
	$grand['deleteImg']			= true;									// delete Images
	
	// Flash settings
	$grand['skinsPath']			= '/'.GRAND_FOLDER.'/skins/';

	// Image Settings
	$grand['imgWidth']			= 800;  								// Image Width
	$grand['imgHeight']			= 600;  								// Image height
	$grand['imgQuality']		= 90;									// Image Quality
	
	$grand['advanced']				= false;  							// Advanced options

	update_option('grand_options', $grand);

}

/**
 * creates all tables for the plugin
 * called during register_activation hook
 * 
 * @access internal
 * @return void
**/
function grand_install () {
	global $wpdb, $wp_roles, $wp_version;

	// Check for capability
	if ( !current_user_can('activate_plugins') ) 
		return;

	// upgrade function changed in WordPress 2.3	
	require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
	
	// add charset & collate like wp core
	$charset_collate = '';

	if ( $wpdb->supports_collation() ) {
		if ( ! empty($wpdb->charset) )
			$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
		if ( ! empty($wpdb->collate) )
			$charset_collate .= " COLLATE $wpdb->collate";
	}
		
	$grand_media	= $wpdb->prefix . 'grand_media';
	$grand_gallery	= $wpdb->prefix . 'grand_gallery';
   
	if($wpdb->get_var("show tables like '$grand_media'") != $grand_media) {

		$sql = "CREATE TABLE {$grand_media} (
		id BIGINT(20) NOT NULL AUTO_INCREMENT,
		file VARCHAR(255) NOT NULL,
		type VARCHAR(128) NOT NULL DEFAULT '',
		title MEDIUMTEXT NULL,
		description TEXT NULL,
		link MEDIUMTEXT NULL,
		date DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
		gallery_in  LONGTEXT NULL,
		sortorder INT(11) NOT NULL DEFAULT '0',
		author BIGINT(20) NOT NULL DEFAULT '0',
		private TINYINT NULL DEFAULT '0',
		rating FLOAT NOT NULL DEFAULT '0',
		hitcounter BIGINT(20) NOT NULL DEFAULT '0',
		data LONGTEXT NULL,
		PRIMARY KEY  id (id)
		) {$charset_collate}";
	
      dbDelta($sql);
    }

	if($wpdb->get_var("show tables like '$grand_gallery'") != $grand_gallery) {
      
		$sql = "CREATE TABLE {$grand_gallery} (
		id BIGINT(20) NOT NULL AUTO_INCREMENT,
		type VARCHAR(128) NOT NULL DEFAULT '',
		title MEDIUMTEXT NULL,
		description MEDIUMTEXT NULL,
		sortorder INT(11) NOT NULL DEFAULT '0',
		author BIGINT(20) NOT NULL DEFAULT '0',
		settings LONGTEXT NULL,
		data LONGTEXT NULL,
		PRIMARY KEY  id (id)
		) {$charset_collate}";
	
      dbDelta($sql);
    }

	// check one table again, to be sure
	if($wpdb->get_var("show tables like '$grand_media'")!= $grand_media) {
		update_option( "grand_init_check", __('GRAND Media: Tables could not created, please check your database settings','gmLang') );
		return;
	}

	$options = get_option('grand_options');
	// set the default settings, if we didn't upgrade
	if ( empty( $options ) )	
 		grand_default_options();
 	
	
	// if all is passed, save the DBVERSION
	add_option("grand_db_version", GRAND_DBVERSION);
}

/**
 * Uninstall all settings and tables
 * Called via Setup and register_unstall hook
 * 
 * @access internal
 * @return void
 */
function flag_uninstall() {
	global $wpdb;
	
	// first remove all tables
	$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}grand_media");
	$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}grand_gallery");
	
	// then remove all options
	delete_option( 'grand_options' );
	delete_option( 'grand_db_version' );

}



?>
